<?php

class Port_controller extends CI_Controller {

    function __construct(){
        parent::__construct();
        //$this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
    }

    function index(){

        $section = "port";
        $cfer = new Cfer(array(
        'Trang chủ' => 'dashboard',
        'Cảng' => base_url('port')));
        $title = _LB_PORT;

        $this->data['page_title'] = 'Cảng';

        $filter = array();
        $filter['name'] = $this->input->post('name');
        $port = Port::getList($filter);

        $this->data['port'] = $port;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);

        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;

        $this->load->view('port_list', $this->data);
    }

    function form() {
        $id = $this->input->post('id');
        $section = "port";
        if ($id) {
            $cfer = new Cfer(array(
            'Cảng' => 'port',
            'Sửa thông tin cảng' => base_url('port/form/?id='.$id)));
        } else {
            $cfer = new Cfer(array(
            'Cảng' => 'port',
            'Thêm cảng mới' => base_url('port/form/?')));
        }
        
        $act = $this->input->post('act');
        $url = $this->input->post('url');
        $target = $this->input->post('target');
    
        $port = new Port();

        if ($id && !$port->get($id)) {
                redirect(base_url('port'));
        }

        if ($act == ACT_SUBMIT) {

            $obj = Facebook::getFanPage($url);

            if ($obj) {
                if ($fan_page = FacebookFanPage::updateData($obj)) {
                    $port->name = $fan_page->name;
                    $port->image = $fan_page->picture;
                    $port->url = $fan_page->link;
                    $port->description = $fan_page->description;
                }
            }

            if ($start_date && trim($start_date) != '')
                $port->start_date = $start_date;

            $port->country = $countries;

            if (MessageHandler::countError() == 0) {
                if (!$id_campaign) {
                    $port->status = CREATED;
                    $port->click_target = $target;
                    $port->created_date = date('Y-m-d h:i:s');
                    $port->id_user = 1;
                    $port->id_network = 1;

                    if ($port->validateInput()) {
                        if ($port->insert())
                            redirect(base_url('campaign'));
                    }

                }
                else {
                    if ($port->status == CREATED)
                        $port->click_target = $target;

                    if ($port->validateInput())
                        if ($port->update())
                            redirect(base_url('campaign'));
                }
            }
        }

        $country = new Country();
       
        $this->data['port'] = $port;
        $this->data['country'] = $country->getAll();
        $this->data['back'] = base_url('port');
        $this->data['section'] = $section;
        
        $this->load->view('port_form', $this->data);
    }
}
